package oocl.eshop.service.impl;

import java.io.File;
import java.util.List;

import org.apache.tomcat.util.http.fileupload.FileItem;

import oocl.eshop.dao.MerchantDao;
import oocl.eshop.dao.PermissionDao;
import oocl.eshop.dao.impl.MerchantDaoImpl;
import oocl.eshop.dao.impl.PermissionDaoImpl;
import oocl.eshop.po.Merchant;
import oocl.eshop.po.Permission;
import oocl.eshop.service.MerchantManager;

public class MerchantManagerImpl implements MerchantManager {
	MerchantDao merchantDao = new MerchantDaoImpl();
	PermissionDao permissonDao = new PermissionDaoImpl();

	@Override
	public Merchant loginSystem(String username, String password) {
		Merchant merchant = merchantDao.findMerchantByTel(username);
		if (merchant == null) {
			return null;
		}
		if (password.equals(merchant.getmPassword())) {
			Permission permi = permissonDao.findPermisson(merchant.getmId());
			merchant.setmStatus(permi.getmStatus());
			return merchant;
		}
		return null;
	}

	@Override
	public int updateMerchant(Merchant merchant) {
		// TODO Auto-generated method stub

		return merchantDao.updateMerchant(merchant);
	}

	@Override
	public Merchant findMerchantById(int mId) {
		// TODO Auto-generated method stub
		Merchant merchant = merchantDao.findMerchantById(mId);
		if (merchant == null) {
			return null;
		}
		Permission permi = permissonDao.findPermisson(merchant.getmId());
		merchant.setmStatus(permi.getmStatus());
		return merchant;
	}

	@Override
	public Merchant LoadFileMerchant(List<FileItem> fis, Merchant merchant) {
		String mPersonName = null;
		String mTel = null;
		String mIdCard = null;
		String mPassword = null;
		String mBrand = null;
		String mLocation = null;
		String mCardPath = null;
		String mLogoPath = null;
		if (merchant != null)
			mTel = merchant.getmTel();
		for (FileItem fileItem : fis) {
			if (fileItem.isFormField()) {
				System.out.println(fileItem.getFieldName());
				System.out.println(fileItem.getString());
				switch (fileItem.getFieldName()) {
				case "mPersonName":
					mPersonName = fileItem.getString();
					break;
				case "mTel":
					mTel = fileItem.getString();
					break;
				case "mIdCard":
					mIdCard = fileItem.getString();
					break;
				case "mPassword":
					mPassword = fileItem.getString();
					break;
				case "mBrand":
					mBrand = fileItem.getString();
					break;
				case "mLocation":
					mLocation = fileItem.getString();
					break;
				default:
					break;
				}

			} else {
				String fileName = fileItem.getName();
				if (fileName.lastIndexOf(".") == -1) {
					continue;
				}
				fileName = fileName.substring(fileName.lastIndexOf("."));
				System.out.println(fileName);
				try {
					File file = new File("D:/Kinva/" + mTel);
					if (!file.exists()) {
						file.mkdirs();
					}
					if (fileItem.getFieldName().equals("mCardPath")) {
						mCardPath = "http://10.222.232.159/" + mTel + "/IdCard" + fileName;
						fileItem.write(new File("D:/Kinva/" + mTel + "/IdCard" + fileName));
					} else {
						mLogoPath = "http://10.222.232.159/" + mTel + "/Logo" + fileName;
						fileItem.write(new File("D:/Kinva/" + mTel + "/Logo" + fileName));
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		Merchant merchant1 = new Merchant(mPersonName, mTel, mPassword, mIdCard, mCardPath, mLocation, mBrand,
				mLogoPath);
		return merchant1;
	}

}
